﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace Common
{
	/// <summary>
	/// 对ListView点击列标题自动排序功能
	/// </summary>
	public class ListViewHelper
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		public ListViewHelper()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		public static void ListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
		{
			System.Windows.Forms.ListView lv = sender as System.Windows.Forms.ListView;
			// 检查点击的列是不是现在的排序列.
			if (e.Column == (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn)
			{
				// 重新设置此列的排序方法.
				if ((lv.ListViewItemSorter as ListViewColumnSorter).Order == System.Windows.Forms.SortOrder.Ascending)
				{
					(lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Descending;
				}
				else
				{
					(lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Ascending;
				}
			}
			else
			{
				// 设置排序列，默认为正向排序
				(lv.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;
				(lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Ascending;
			}
			// 用新的排序方法对ListView排序
			((System.Windows.Forms.ListView)sender).Sort();
		}
	}

	/// <summary>
	/// 继承自IComparer
	/// </summary>
	public class ListViewColumnSorter : System.Collections.IComparer
	{
		/// <summary>
		/// 指定按照哪个列排序
		/// </summary>
		private int ColumnToSort;
		/// <summary>
		/// 指定排序的方式
		/// </summary>
		private System.Windows.Forms.SortOrder OrderOfSort;
		/// <summary>
		/// 声明CaseInsensitiveComparer类对象
		/// </summary>
		private System.Collections.CaseInsensitiveComparer ObjectCompare;

		/// <summary>
		/// 构造函数
		/// </summary>
		public ListViewColumnSorter()
		{
			// 默认按第一列排序
			ColumnToSort = 0;

			// 排序方式为不排序
			OrderOfSort = System.Windows.Forms.SortOrder.None;

			// 初始化CaseInsensitiveComparer类对象
			ObjectCompare = new System.Collections.CaseInsensitiveComparer();
		}

		/// <summary>
		/// 重写IComparer接口.
		/// </summary>
		/// <param name="x">要比较的第一个对象</param>
		/// <param name="y">要比较的第二个对象</param>
		/// <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
		public int Compare(object x, object y)
		{
			int compareResult;
			System.Windows.Forms.ListViewItem listviewX, listviewY;

			// 将比较对象转换为ListViewItem对象
			listviewX = (System.Windows.Forms.ListViewItem)x;
			listviewY = (System.Windows.Forms.ListViewItem)y;

			string xText = listviewX.SubItems[ColumnToSort].Text;
			string yText = listviewY.SubItems[ColumnToSort].Text;

			int xInt, yInt;

			// 比较,如果值为IP地址，则根据IP地址的规则排序。
			if (IsIP(xText) && IsIP(yText))
			{
				compareResult = CompareIp(xText, yText);
			}
			else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
			{
				//比较数字
				compareResult = CompareInt(xInt, yInt);
			}
			else
			{
				//比较对象
				compareResult = ObjectCompare.Compare(xText, yText);
			}

			// 根据上面的比较结果返回正确的比较结果
			if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
			{
				// 因为是正序排序，所以直接返回结果
				return compareResult;
			}
			else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
			{
				// 如果是反序排序，所以要取负值再返回
				return (-compareResult);
			}
			else
			{
				// 如果相等返回0
				return 0;
			}
		}

		/// <summary>
		/// 判断是否为正确的IP地址，IP范围（0.0.0.0～255.255.255）
		/// </summary>
		/// <param name="ip">需验证的IP地址</param>
		/// <returns></returns>
		public bool IsIP(String ip)
		{
			return System.Text.RegularExpressions.Regex.Match(ip, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$").Success;
		}

		/// <summary>
		/// 比较两个数字的大小
		/// </summary>
		/// <param name="ipx">要比较的第一个对象</param>
		/// <param name="ipy">要比较的第二个对象</param>
		/// <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
		private int CompareInt(int x, int y)
		{
			if (x > y)
			{
				return 1;
			}
			else if (x < y)
			{
				return -1;
			}
			else
			{
				return 0;
			}
		}

		/// <summary>
		/// 比较两个IP地址的大小
		/// </summary>
		/// <param name="ipx">要比较的第一个对象</param>
		/// <param name="ipy">要比较的第二个对象</param>
		/// <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
		private int CompareIp(string ipx, string ipy)
		{
			string[] ipxs = ipx.Split('.');
			string[] ipys = ipy.Split('.');

			for (int i = 0; i < 4; i++)
			{
				if (Convert.ToInt32(ipxs[i]) > Convert.ToInt32(ipys[i]))
				{
					return 1;
				}
				else if (Convert.ToInt32(ipxs[i]) < Convert.ToInt32(ipys[i]))
				{
					return -1;
				}
				else
				{
					continue;
				}
			}
			return 0;
		}

		/// <summary>
		/// 获取或设置按照哪一列排序.
		/// </summary>
		public int SortColumn
		{
			set
			{
				ColumnToSort = value;
			}
			get
			{
				return ColumnToSort;
			}
		}

		/// <summary>
		/// 获取或设置排序方式.
		/// </summary>
		public System.Windows.Forms.SortOrder Order
		{
			set
			{
				OrderOfSort = value;
			}
			get
			{
				return OrderOfSort;
			}
		}
	}


	internal class Mp3TagID3V1
	{
		//流派分类，共有148种
		readonly string[] GENRE = {"Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge","Hip-Hop",
            "Jazz","Metal","New Age","Oldies","Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", 
            "Industrial", "Alternative"
            };

		private string title = string.Empty;
		/// <summary>
		/// 标题
		/// </summary>
		public string Title
		{
			get { return title; }
		}

		private string artist = string.Empty;
		/// <summary>
		/// 艺术家，演唱者
		/// </summary>
		public string Artist
		{
			get { return artist; }
		}

		private string album = string.Empty;
		/// <summary>
		/// 所属专辑
		/// </summary>
		public string Album
		{
			get { return album; }
		}

		private string pubYear = string.Empty;
		/// <summary>
		/// 发行年份
		/// </summary>
		public string PublishYear
		{
			get { return pubYear; }
		}

		private string comment = string.Empty;
		/// <summary>
		/// 备注、说明
		/// </summary>
		public string Comment
		{
			get
			{
				if (comment.Length == 30)
				{
					//如果是 ID3 V1.1的版本，那么comment只占前28个byte，第30个byte存放音轨信息
					if (TagVersion(comment)) return comment.Substring(0, 28).TrimEnd();
				}

				return comment.TrimEnd();
			}
		}

		/// <summary>
		/// 音轨
		/// </summary>
		public string Track
		{
			get
			{
				if (comment.Length == 30)
				{
					//如果是 ID3 V1.1的版本，读取音轨信息
					if (TagVersion(comment)) return ((int)comment[29]).ToString();
				}

				return string.Empty;
			}
		}

		private string genre;
		/// <summary>
		/// 流派
		/// </summary>
		public string Genre
		{
			get { return genre; }
		}

		/// <summary>
		/// 判断MP3的TAG信息的版本，是V1.0 还是 V1.1
		/// </summary>
		/// <param name="comment"></param>
		/// <returns>true表示是 1.1，false表示是 1.0</returns>
		private bool TagVersion(string comment)
		{
			if (comment[28].Equals('\0') && comment[29] > 0 || comment[28] == 32 && comment[29] != 32)
				return true;
			return false;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="mp3FilePath">MP3文件的完整路径</param>
		public Mp3TagID3V1(string mp3FilePath)
		{
			byte[] tagBody = new byte[128];
			string tagFlag;

			if (!File.Exists(mp3FilePath))
				throw new FileNotFoundException("指定的MP3文件不存在！", mp3FilePath);

			//读取MP3文件的最后128个字节的内容
			using (FileStream fs = new FileStream(mp3FilePath, FileMode.Open, FileAccess.Read))
			{
				fs.Seek(-128, SeekOrigin.End);
				fs.Read(tagBody, 0, 128);
				fs.Close();
			}

			//取TAG段的前三个字节
			tagFlag = Encoding.Default.GetString(tagBody, 0, 3);

			//如果没有TAG信息，则直接返回
			if (!"TAG".Equals(tagFlag, StringComparison.InvariantCultureIgnoreCase))
			{
				return;
				//throw new InvalidDataException("指定的MP3文件没有TAG信息！");
			}

			char[] invalidChars = new char[] { ' ', '\0' };
			//按照MP3 ID3 V1 的tag定义，依次读取相关的信息
			this.title = Encoding.Default.GetString(tagBody, 3, 30).Trim().Replace("\0","");
			this.artist = Encoding.Default.GetString(tagBody, 33, 30).Trim().Replace("\0", "");
			this.album = Encoding.Default.GetString(tagBody, 62, 30).Trim().Replace("\0", "");
			this.pubYear = Encoding.Default.GetString(tagBody, 93, 4).Trim().Replace("\0", "");
			this.comment = Encoding.Default.GetString(tagBody, 97, 30).Trim().Replace("\0", "");
			Int16 g = (Int16)tagBody[127];
			this.genre = g >= GENRE.Length ? "未知" : GENRE[g];
		}
	}
}